package com.lvmama.controller.rhino.active;

import com.alibaba.fastjson.JSONArray;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.DateUtil;
import com.lvmama.pojo.entity.rhino.active.ActiveUser;
import com.lvmama.service.rhino.active.ActiveUserService;
import com.lvmama.utils.ToolUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/activeUser")
@Slf4j
public class ActiveUserController {

    @Autowired
    private ActiveUserService activeUserService;

    /**
     * @Title: activeUserChartData
     * @Description: 图表数据
     * @param @param chooseTime
     * @param @return 设定文件
     * @return Map<String,Object> 返回类型
     * @throws
     */
    @PostMapping("/activeUserChartData")
    @ResponseBody
    public Result<Map<String, Object>> visitorChartData(@RequestParam("chooseTime")String chooseTime) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ActiveUser user = new ActiveUser();
        // 查询日期条件处理
        Map<String, Date> dateMap = ToolUtils.getQueryDate(chooseTime);
        Date beginDate = dateMap.get("beginDate");
        Date endDate = dateMap.get("endDate");
        user.setStartDate(beginDate);
        user.setEndDate(endDate);
        try {
            List<ActiveUser> activeUserListByDate = activeUserService.getUserListByDate(user);
            List<String> platformList = new ArrayList<String>();
            platformList.add("PC");
            platformList.add("IOS");
            platformList.add("Android");
            platformList.add("WAP");
            platformList.add("OTH");
            platformList.add("ALL");
            // 折线图数据
            String[] dateAxis = DateUtil.getBetweenTime(beginDate, endDate, "yyyy/MM/dd");
            List<String> dateList = Arrays.asList(dateAxis);
            JSONArray dauArr = new JSONArray();
            JSONArray mauArr = new JSONArray();
            for (String date : dateList) {
                for (String platform : platformList) {
                    int dau = 0;
                    int mau = 0;
                    for (ActiveUser acUser : activeUserListByDate) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                        String time = sdf.format(acUser.getOperDate());
                        if (acUser.getPlatform().equals(platform) && time.equals(date)) {
                            dau = acUser.getDau();
                            mau = acUser.getMau();
                        }
                    }
                    JSONObject o = new JSONObject();
                    o.put("platform", platform);
                    o.put("time", date);
                    o.put("dau", dau);
                    dauArr.add(o);

                    JSONObject o2 = new JSONObject();
                    o2.put("platform", platform);
                    o2.put("time", date);
                    o2.put("mau", mau);
                    mauArr.add(o2);
                }
            }
            resultMap.put("platformList", platformList);
            resultMap.put("dauArrData", dauArr);
            resultMap.put("mauArrData", mauArr);
        } catch (Exception e) {
            log.error("visitorChartData error{}",e);
        }
        return Result.success(resultMap);
    }



}
